package com.zqweb.gulimall.product.service.impl;

import com.zqweb.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqweb.common.utils.PageUtils;
import com.zqweb.common.utils.Query;

import com.zqweb.gulimall.product.dao.BrandDao;
import com.zqweb.gulimall.product.entity.BrandEntity;
import com.zqweb.gulimall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");// 挂载key参数的具体搜索
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.like("brand_id",key).or().like("name",key);
        }
        IPage<BrandEntity> page = this.page(
            new Query<BrandEntity>().getPage(params),
            wrapper
        );
        return new PageUtils(page);
    }

    @Override
    /**
     * 修改品牌信息时，若修改的字段在其他表中也有记录，同样需要将关联表中的相应字段记录进行修改
     */
    @Transactional
    public void updateDetail(BrandEntity brand) {
        Long brandId = brand.getBrandId();
        this.updateById(brand);
        String name = brand.getName();
        if(!StringUtils.isEmpty(name)){
            categoryBrandRelationService.updateBrand(brandId,name);
        }
    }

    @Override
    public List<BrandEntity> brandIdsInfo(List<Long> brandIds) {
        List<BrandEntity> brandIdList = this.baseMapper.selectList(new QueryWrapper<BrandEntity>().in("brand_id", brandIds));
        return brandIdList;
    }
}